package com.cloud.utils;

import cn.hutool.core.codec.Base64Encoder;
import com.cloud.common.domain.vo.SvgParam;
import org.apache.batik.anim.dom.SAXSVGDocumentFactory;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;
import org.apache.batik.transcoder.image.PNGTranscoder;
import org.apache.batik.util.XMLResourceDescriptor;
import org.apache.commons.lang3.ObjectUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.ByteArrayOutputStream;
import java.util.Map;

/**
 * SVG操作工具
 *
 * @author yzj
 */
public class SVGUtils {

    /**
     * SVG转PNG并设置颜色
     */
    public static String svgInvertUrl(SvgParam svgParam) {
        String pngBaseStr = null;
        ByteArrayOutputStream outputStream = getOutputStream(svgParam);
        if (ObjectUtils.isNotEmpty(outputStream)) {
            Base64Encoder encoder = new Base64Encoder();
            String xmlStr = encoder.encode(outputStream.toByteArray());
            pngBaseStr = "data:image/png;base64," + xmlStr;
        }
        return pngBaseStr;
    }

    /**
     * SVG转XML字符串
     */
    public static String svgInvertXml(SvgParam svgParam) {
        ByteArrayOutputStream outputStream = null;
        try {
            Document document = getSvgDocument(svgParam);
            if (ObjectUtils.isNotEmpty(document)) {
                TransformerFactory transFactory = TransformerFactory.newInstance();
                Transformer trans = transFactory.newTransformer();
                // 解决中文问题
                trans.setOutputProperty("encoding", "GB23121");
                outputStream = new ByteArrayOutputStream();
                trans.transform(new DOMSource(document), new StreamResult(outputStream));
                outputStream.flush();
                outputStream.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return outputStream.toString();
    }

    /**
     * 获取SVG XML对象
     */
    public static Document getSvgDocument(SvgParam svgParam) {
        Document document = null;
        try {
            String parser = XMLResourceDescriptor.getXMLParserClassName();
            SAXSVGDocumentFactory factory = new SAXSVGDocumentFactory(parser);
            document = factory.createDocument(svgParam.getSvgUrl());
            for (Map.Entry<String, String> entry : svgParam.getSvgMap().entrySet()) {
                Element result = document.getElementById(entry.getKey());
                if (ObjectUtils.isNotEmpty(result)) {
                    result.setAttribute("fill", entry.getValue());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return document;
    }

    /**
     * 获取文件输出字节流
     */
    public static ByteArrayOutputStream getOutputStream(SvgParam svgParam) {
        ByteArrayOutputStream outputStream = null;
        try {
            Document document = getSvgDocument(svgParam);
            if (ObjectUtils.isNotEmpty(document)) {
                TranscoderInput input = new TranscoderInput(document);
                outputStream = new ByteArrayOutputStream();
                PNGTranscoder pngTrans = new PNGTranscoder();
                // SVG格式转PNG格式
                pngTrans.transcode(input, new TranscoderOutput(outputStream));
            }
            if (ObjectUtils.isNotEmpty(outputStream)) {
                outputStream.flush();
                outputStream.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return outputStream;
    }

}
